<?php

namespace App\Http\Resources;

use App\Models\Reply;

class ReplyResource
{

    /**
     * 格式化评论列表
     * @param $topicId
     * @param int $page
     * @param int $perPage
     * @return array
     */
    public function format($topicId, $page = 1, $perPage = 10)
    {

        $list = (new Reply)->getPaginate(['topic_id' => $topicId], ['user', 'toUser'], $page, $perPage)['list'];
        $returnData = [];
        foreach ($list as $k => $v) {
            $returnData[$k] = [
                'id' => $v['id'],
                'topic_id' => $v['topic_id'],
                'user_id' => $v['user_id'],
                'content' => $v['content'],
                'is_adopt' => $v['is_adopt'],
                'created_at' => $v['created_at'],
                'user_name' => $v['user']['name'],
                'user_head_img' => $v['user']['http_head_img'],
                'to_user_name' => $v['to_user']['name'] ?? '',
                'to_user_id' => $v['to_user']['id'] ?? 0,
                'has_adopt' => $list[0]['is_adopt'] == 1 ?? false
            ];
        }

        return $returnData;
    }
}
